//某班级考试成绩按非严格递增顺序记录于整数数组 scores，请返回目标成绩 target 的出现次数。 
//
// 
//
// 示例 1： 
//
// 
//输入: scores = [2, 2, 3, 4, 4, 4, 5, 6, 6, 8], target = 4
//输出: 3 
//
// 示例 2： 
//
// 
//输入: scores = [1, 2, 3, 5, 7, 9], target = 6
//输出: 0 
//
// 
//
// 提示： 
//
// 
// 0 <= scores.length <= 10⁵ 
// -10⁹ <= scores[i] <= 10⁹ 
// scores 是一个非递减数组 
// -10⁹ <= target <= 10⁹ 
// 
//
// 
//
// 注意：本题与主站 34 题相同（仅返回值不同）：https://leetcode-cn.com/problems/find-first-and-last-
//position-of-element-in-sorted-array/ 
//
// 
//
// Related Topics 数组 二分查找 👍 462 👎 0


package LeetCode.editor.cn;


/**
 * @author ldltd
 * @date 2025-05-16 19:07:51
 * @description LCR 172.统计目标成绩的出现次数
 
 */
 
public class ZaiPaiXuShuZuZhongChaZhaoShuZiLcof {
    public static void main(String[] args) {
    //测试代码
    ZaiPaiXuShuZuZhongChaZhaoShuZiLcof fun = new ZaiPaiXuShuZuZhongChaZhaoShuZiLcof();
    Solution solution= fun.new Solution();
        System.out.println(solution.countTarget(new int[]{1},1));
    }

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        //二分找到这个数，然后扩散计数
    public int countTarget(int[] scores, int target) {
        if (scores == null || scores.length == 0) return 0;

        int l = 0, r = scores.length - 1, m;
        while (l <= r) {
            m = l + (r - l) / 2;
            if (scores[m] < target) {
                l = m + 1;
            } else if (scores[m] > target) {
                r = m - 1;
            } else {
                // 找到目标后向左右扩展
                l = m;
                r = m;
                while (l - 1 >= 0 && scores[l - 1] == target) {
                    l--;
                }
                while (r + 1 < scores.length && scores[r + 1] == target) {
                    r++;
                }
                return r - l + 1;
            }
        }
        return 0;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
